package hardware;




import graphics.Bitmap2D;
import graphics.Font5x5;
import graphics.Font8x8;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

//import System.Settings_System;




import display.Display;
import display.LEDBar_Side;
import display.LEDBar_Top;
import display.Panel_Help;
import display.Panel_LED;
import display.RGBColor;


public class Display2D extends JFrame implements FocusListener, KeyListener, ActionListener{

	/**
		 * 
		 */
private static final long serialVersionUID = 1L;

JDesktopPane MyDesktopPane =new JDesktopPane(); 

private static Display MyDisplay;
private static LEDBar_Top MyTopLedBar;
private static LEDBar_Side mySideLedBar;

static RGBColor  displayArea[][]=new RGBColor [Display.xSize] [Display.ySize];	
static RGBColor  ledTopArea[]=new RGBColor [LEDBar_Top.LEDBarSize];	
static RGBColor  ledSideArea[]=new RGBColor [LEDBar_Side.LEDBarSize];	

private static final RGBColor COLOR_BLACK = new RGBColor((short)0);



private static final String [] [] menuNames =
{{"Hardware","Simulated","Real","Settings","System","Remote","Help me!"},

{"Hardware","Simuliert","Real","Einstellungen","System","Remote","Hilf mir!"}};	


private static final int String_HardwareID					=0; 
private static final int String_SimulateID					=String_HardwareID+1;
private static final int String_RealID						= String_SimulateID+1;
private static final int String_SettingsID					= String_RealID+1;
private static final int String_SystemID					= String_SettingsID+1;
private static final int String_RemoteID					= String_SystemID+1;
private static final int String_HelpID						= String_RemoteID+1;


private JMenuBar MainMenuBar = new JMenuBar();

public final static String	MenuCommandSymulatedHardware ="cmd_modeSymulated";
public final static String	MenuCommandRemoteHardware = "cmd_modeReal";

public final static String	MenuCommandSettingsRemote ="cmd_settingsRemote";
public final static String	MenuCommandSettingsSystem = "cmd_SettingsSystem";

public Display2D()
{
		

//	MyDesktopPane.setSize(1200,780);
	setSize(560,480);
	setContentPane(MyDesktopPane);

	setLayout(null);	
		
	MyDisplay= new Display();
	MyDisplay.setLocation(100,50);
	MyDesktopPane.add(MyDisplay);


	MyTopLedBar =new LEDBar_Top();
	MyTopLedBar.setLocation(100,0);
	MyDesktopPane.add(MyTopLedBar);



	mySideLedBar=new LEDBar_Side();
	mySideLedBar.setLocation(500,50);
	MyDesktopPane.add(mySideLedBar);

	
	JPanel tmpPanel;
	tmpPanel= new Panel_Help();
	tmpPanel.setBounds(0, 0, 100, 330);
	MyDesktopPane.add(tmpPanel);
	
	JPanel ledPanel;
	ledPanel= new Panel_LED();
	ledPanel.setBounds(0, 330, 100, 100);
	MyDesktopPane.add(ledPanel);
	
	
	MakeMainMenu();
	addWindowListener(new WindowEventHandler());
	setVisible(true);


	addFocusListener(this);
	addKeyListener(this);


}


void MakeMainMenu()
{
	MainMenuBar.add(MakeHardwareMenu());
	MainMenuBar.add(MakeSettingsMenu());
	MainMenuBar.add(MakeHelpMenu());
	
	setJMenuBar(MainMenuBar);
		
}

	

JMenuItem MakeMenuItem(String MenuItemText,String ActionCommand)
{
	JMenuItem tmpItem;
	
	tmpItem = new JMenuItem(MenuItemText);
	tmpItem.setActionCommand(ActionCommand);
	tmpItem.addActionListener(this);
	return(tmpItem);
}


JMenuItem MakeCheckboxMenuItem(String MenuItemText,String ActionCommand)
{
	JCheckBoxMenuItem  tmpItem;
	
	tmpItem = new JCheckBoxMenuItem (MenuItemText);
	tmpItem.setActionCommand(ActionCommand);
	tmpItem.addActionListener(this);
	return(tmpItem);
}



JMenu MakeHardwareMenu()
{
	JMenu	tmpMenu;
	JMenuItem tmpMenuItem;
	
	tmpMenu = new JMenu(menuNames [0] [String_HardwareID]);
	tmpMenuItem=MakeCheckboxMenuItem(menuNames [0] [String_SimulateID],MenuCommandSymulatedHardware);
	
	tmpMenu.add(tmpMenuItem);
	tmpMenu.add(MakeCheckboxMenuItem(menuNames [0] [String_RealID],MenuCommandRemoteHardware));
	

	return(tmpMenu);	
}







JMenu MakeSettingsMenu()
{
	JMenu	tmpMenu;
	JMenuItem tmpMenuItem;
	
	tmpMenu = new JMenu(menuNames [0] [String_SettingsID]);
	tmpMenuItem=MakeMenuItem(menuNames [0] [String_SystemID],MenuCommandSettingsSystem);
	
	tmpMenu.add(tmpMenuItem);
	
	tmpMenu.add(MakeMenuItem(menuNames [0] [String_RemoteID],MenuCommandSettingsRemote));
	return(tmpMenu);	
}


JMenu MakeHelpMenu()
{
	JMenu	tmpMenu;
	JMenuItem tmpMenuItem;
	
	tmpMenu = new JMenu(menuNames [0] [String_HelpID]);
	//tmpMenuItem=MakeMenuItem(menuNames [0] [String_SystemID],MenuCommandSettingsSystem);
	
//	tmpMenu.add(tmpMenuItem);
	//TmpMenu.add(MakeMenuItem(menuNames [0] [String_SimulateID],MenuCommandSymulatedHardware));
	
	//tmpMenu.add(MakeMenuItem(menuNames [0] [String_RemoteID],MenuCommandSettingsRemote));
	return(tmpMenu);	
}


public static int getRowCount()
{
	return(display.Display.ySize);
}

public static int getColumnCount()
{
	return(display.Display.xSize);
}


public static void setPixel(int xPos,int yPos,short color)
{
if ((xPos>=0) && (yPos>=0) && (xPos<Display.xSize) && (yPos<Display.ySize))
	{
	displayArea[xPos][yPos]=new RGBColor((short)color);
	}
	
}


public static void clear()
{
int xCounter;
int yCounter;

for (xCounter=0;xCounter<Display.xSize;xCounter++)
	for (yCounter=0;yCounter<Display.ySize;yCounter++)
	{
		displayArea[xCounter][yCounter]= COLOR_BLACK;
	}	
	
	for (xCounter=0;xCounter<ledTopArea.length;xCounter++)
		{
		ledTopArea[xCounter]=COLOR_BLACK;
		}
	
	for (xCounter=0;xCounter<ledSideArea.length;xCounter++)
		{
		ledSideArea[xCounter]=COLOR_BLACK;
		}
}



public static void setSideLED(int index, short color)
{
	if ((index>=0) && (index <ledSideArea.length))
	{
	ledSideArea[index]=new RGBColor(color);
	}
}


public static void  setTopLED(int index, short color)
{
	if ((index>=0) && (index <ledTopArea.length))
		{
		ledTopArea[index]=new RGBColor(color);
		}
}





public static void doRepaint()
{
	int xCounter;
	int yCounter;
	
	for (xCounter=0;xCounter<Display.xSize;xCounter++)
		for (yCounter=0;yCounter<Display.ySize;yCounter++)
		{
			Display.setPixel(xCounter, yCounter, displayArea[xCounter][yCounter]);
		}
	
	for (xCounter=0;xCounter<ledTopArea.length;xCounter++)
		{
		LEDBar_Top.setLEDColor(xCounter, ledTopArea[xCounter]);
		}

	for (xCounter=0;xCounter<ledSideArea.length;xCounter++)
		{
		LEDBar_Side.setLEDColor(xCounter, ledSideArea[xCounter]);
		}

	
}



public static void drawLine(int x,int y,int dx,int dy,short color)
{

		int counter;
		int tmpPoint;

		int xa;
		int ya;


		if (dx<0)
			{
			xa=-dx;
			}
		else
			{
			xa=dx;
			}

		if (dy<0)
			{
			ya=-dy;
			}
		else
			{
			ya=dy;
			}


		Display2D.setPixel(x,y,color);

		if (xa>ya)
		{
			for (counter=1;counter<=xa;counter++)
				{
				tmpPoint=y+(dy*(counter)/xa);
				if (dx>=0)
					{
					Display2D.setPixel(x+counter,tmpPoint,color);
					}
				else
					{
					Display2D.setPixel(x-counter,tmpPoint,color);
					}
				}
		}

		else
			{
			for (counter=1;counter<=ya;counter++)
				{
				tmpPoint=x+(dx*(counter)/ya);
				if (dy>=0)
					{
					Display2D.setPixel(tmpPoint,y+counter,color);
					}
				else
					{
					Display2D.setPixel(tmpPoint,y-counter,color);
					}
				}

			}



}


public static boolean print8x8Char(char index,short color,int x, int y)
{
	int xCounter;
	int yCounter;
	byte dataLine;
	
	index-=32;
	
	if ((x < -7) || (x>7) || (y < -7) || (y>7))
		return(false);
		
	if (index>=Font8x8.font8x8.length)
		return(false);
	
	for (yCounter=0;yCounter<8;yCounter++)
	{
		dataLine=Font8x8.font8x8[index] [7-yCounter];

		for (xCounter=7;xCounter>=0;xCounter--)
		{	
			if ((dataLine &0x1) >0)
				{
				Display2D.setPixel(x+ xCounter, y+ yCounter, color);
				}
			dataLine>>=1;
		}
	}
	
	
	return(true);



}




public static boolean print5x5Char(char index,short color,int x, int y)
{
	int xCounter;
	int yCounter;
	byte dataLine;
	
	index-=32;
	
	if ((x < -4) || (x>7) || (y < -4) || (y>7))
		return(false);
		
	if (index>=Font5x5.font5x5.length)
		return(false);
	
	for (yCounter=0;yCounter<5;yCounter++)
	{
		dataLine=Font5x5.font5x5[index] [4-yCounter];

		for (xCounter=4;xCounter>=0;xCounter--)
		{	
			if ((dataLine &0x1) >0)
				{
				Display2D.setPixel(x+ xCounter, y+ yCounter, color);
				}
			dataLine>>=1;
		}
	}
	
	
	return(true);
}



public static void  display2DBitmap(int xPos, int yPos, Bitmap2D bitmap)
{
	int xCounter;
	int yCounter;


	
	for (yCounter=0;yCounter<bitmap.getYSize();yCounter++){
		for (xCounter=0;xCounter<bitmap.getXSize();xCounter++)
		{
		Display2D.setPixel(xPos+xCounter, yPos+yCounter, bitmap.data[yCounter] [xCounter]);		

		}
	}
}


@Override
public void focusGained(FocusEvent arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public void focusLost(FocusEvent arg0) {
	requestFocus();
	// TODO Auto-generated method stub
	
}




	@Override
	public void keyPressed(KeyEvent arg0) {
		
		switch (arg0.getKeyCode())
			{
			case KeyEvent.VK_A:
				UI.keyLLeft=true;
				UI.keyLLeftFlank=true;
				break;

			case KeyEvent.VK_D:
				UI.keyLRight=true;
				UI.keyLRightFlank=true;
				break;

			case KeyEvent.VK_W:
				UI.keyLUp=true;
				UI.keyLUpFlank=true;
				break;

			case KeyEvent.VK_S:
				UI.keyLDown=true;
				UI.keyLDownFlank=true;
				break;

			case KeyEvent.VK_LEFT:
				UI.keyRLeft=true;
				UI.keyRLeftFlank=true;
				break;

			case KeyEvent.VK_RIGHT:
				UI.keyRRight=true;
				UI.keyRRightFlank=true;
				break;

			case KeyEvent.VK_UP:
				UI.keyRUp=true;
				UI.keyRUpFlank=true;
				break;

			case KeyEvent.VK_DOWN:
				UI.keyRDown=true;
				UI.keyRDownFlank=true;
				break;

				
			// ACC
				
			case KeyEvent.VK_J:
				UI.accXValue=+15;
				break;

			case KeyEvent.VK_L:
				UI.accXValue=-15;
				break;

			case KeyEvent.VK_I:
				UI.accYValue=-15;
				break;

			case KeyEvent.VK_K:
				UI.accYValue=15;
				break;

				
				}
		}

	@Override
	public void keyReleased(KeyEvent arg0) {
			
		switch (arg0.getKeyCode())
			{
			case KeyEvent.VK_A:
				UI.keyLLeft=false;
				UI.keyLLeftFlank=false;
				break;

			case KeyEvent.VK_D:
				UI.keyLRight=false;
				UI.keyLRightFlank=false;
				break;

			case KeyEvent.VK_W:
				UI.keyLUp=false;
				UI.keyLUpFlank=false;
				break;

			case KeyEvent.VK_S:
				UI.keyLDown=false;
				UI.keyLDownFlank=false;
				break;
				
			case KeyEvent.VK_LEFT:
				UI.keyRLeft=false;
				UI.keyRLeftFlank=false;
				break;

			case KeyEvent.VK_RIGHT:
				UI.keyRRight=false;
				UI.keyRRightFlank=false;
				break;

			case KeyEvent.VK_UP:
				UI.keyRUp=false;
				UI.keyRUpFlank=false;
				break;

			case KeyEvent.VK_DOWN:
				UI.keyRDown=false;
				UI.keyRDownFlank=false;
				break;				

				// ACC
			
			case KeyEvent.VK_J:
			case KeyEvent.VK_L:
				UI.accXValue=0;
				break;

			case KeyEvent.VK_I:
			case KeyEvent.VK_K:
				UI.accYValue=0;
				break;

			}
		}

	@Override
	public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
		
	}




	class WindowEventHandler extends WindowAdapter 
	{
	public void windowClosing(WindowEvent e)
		{
		// Save Settings (Window Positions, Open Windows etc)
		System.exit(0);
		}
	}




	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

